//
//  GWLeftMenuView.m
//  网易新闻
//
//  Created by qianfeng on 16/2/28.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWLeftMenuView.h"
#import "GWLeftViewBtn.h"
@interface GWLeftMenuView ()
@property (nonatomic,weak)GWLeftViewBtn *selectedBtn;

@property (nonatomic,weak) GWLeftViewBtn *newsBtn;
@end
@implementation GWLeftMenuView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        CGFloat alpha =0.2;
       self.newsBtn = [self btnWithImageName:@"sidebar_nav_news" title:@"新闻" bgColor:myColorRGBA(202, 68, 73, alpha)];
        
        [self btnWithImageName:@"sidebar_nav_reading" title:@"订阅" bgColor:myColorRGBA(190, 111, 69, alpha)];
        [self btnWithImageName:@"sidebar_nav_photo" title:@"图片" bgColor:myColorRGBA(76, 132, 190, alpha)];
        [self btnWithImageName:@"sidebar_nav_video" title:@"视频" bgColor:myColorRGBA(101, 170, 78, alpha)];
        [self btnWithImageName:@"sidebar_nav_comment" title:@"跟帖" bgColor:myColorRGBA(170, 172, 73, alpha)];
        [self btnWithImageName:@"sidebar_nav_radio" title:@"电台" bgColor:myColorRGBA(190, 62, 119, alpha)];
    }
    return self;
}
-(void)setDelegate:(id<GWLeftMenuViewDelegate>)delegate
{
    _delegate =delegate;
    [self btnOnClick:self.newsBtn];
}

-(GWLeftViewBtn *)btnWithImageName:(NSString *)imageName title:(NSString *)title bgColor:(UIColor *)bgColor
{
    GWLeftViewBtn *btn =[[GWLeftViewBtn alloc]init];
    btn.tag =self.subviews.count;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font =[UIFont systemFontOfSize:17];
    
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchDown];
    [btn setBackgroundImage:[UIImage imageWithColor:bgColor] forState:UIControlStateSelected];
    
    [self addSubview:btn];
    btn.titleEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
    btn.contentEdgeInsets =UIEdgeInsetsMake(0, 30, 0, 0);
    btn.adjustsImageWhenHighlighted =NO;
    btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
    return btn;
}

-(void)btnOnClick:(GWLeftViewBtn *)btn
{
    if ([self.delegate respondsToSelector:@selector(leftMenuView:didSeletedView:fromView:)]) {
        [self.delegate leftMenuView:self didSeletedView:btn.tag fromView:self.selectedBtn.tag];
    }
    self.selectedBtn.selected =NO;
    btn.selected =YES;
    self.selectedBtn =btn;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    int btnCount =(int)self.subviews.count;
    CGFloat btnW =self.width;
    CGFloat btnH =self.height/btnCount;
    
    for (int i=0; i<btnCount; i++) {
        GWLeftViewBtn *btn =self.subviews[i];
        btn.frame =CGRectMake(0, btnH*i, btnW, btnH);
    }
    
    
}


@end
